﻿using GN.Pay.Core.Entities;
using GN.Pay.Core.Services;
using GN.Pay.Core.Values;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace GN.Pay.GatewayWeb.JumpNotify
{
    /// <summary>
    /// Hub 会话
    /// </summary>
    public class HubSession
    {
        /// <summary>
        /// 实例化 HubSession 类新实例
        /// </summary>
        /// <param name="applyId">申请Id</param>
        /// <param name="connectionId">连接Id</param>
        /// <param name="client">客户端Id</param>
        public HubSession(string applyId, string connectionId, dynamic client)
        {
            ApplyId = applyId;
            ConnectionId = connectionId;
            Client = client;
        }

        /// <summary>
        /// 获取申请id
        /// </summary>
        public string ApplyId { get; private set; }

        /// <summary>
        /// 获取连接Id
        /// </summary>
        public string ConnectionId { get; private set; }

        /// <summary>
        /// 客户端
        /// </summary>
        public dynamic Client { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="payNotifyData"></param>
        /// <returns></returns>
        private async Task<bool> SendNotify(NotifyMessage message)
        {
            return await Task.Run(() =>
            {
                try
                {
                    Client.notify(message.ToJson(true));
                    return true;
                }
                catch
                {
                    return false;
                }
            });
        }

        private NotifyMessage CreateMessage(TradeNotify tradeNotify, int state)
        {
            if (tradeNotify == null)
            {
                return null;
            }
            var data = tradeNotify.GetPayNotifyData();
            if (data == null)
            {
                return null;
            }
            var service = IocManagerUtils.IocManager.Resolve<ITradeCacheService>();
            var appInfo = service.GetMerchantAppInfo(tradeNotify.AppId);
            var notify = new NotifyMessage()
            {
                AppHomeUrl = appInfo != null ? appInfo.AppHomeUrl : "",
                ApplyId = data.ApplyId,
                TradeNo = data.TradeNo,
                State = state
            };
            return notify;
        }

        /// <summary>
        /// 付款成功通知
        /// </summary>
        /// <param name="payNotifyData">数据</param>
        public async Task<bool> PaySuccess(TradeNotify tradeNotify)
        {
            var msg = CreateMessage(tradeNotify, 0);
            if (msg == null)
            {
                return false;
            }
            return await SendNotify(msg);
        }

        /// <summary>
        /// 通知成功通知
        /// </summary>
        /// <param name="payNotifyData">数据</param>
        public async Task<bool> NotifySuccess(TradeNotify tradeNotify)
        {
            var msg = CreateMessage(tradeNotify, 1);
            if (msg == null)
            {
                return false;
            }
            return await SendNotify(msg);
        }
    }
}